package top.vanchor.myblog2back.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import top.vanchor.myblog2back.entity.BlogBlog;
import top.vanchor.myblog2back.mapper.BlogBlogMapper;
import top.vanchor.myblog2back.service.IBlogBlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2021-11-12
 */
@Service
public class BlogBlogServiceImpl extends ServiceImpl<BlogBlogMapper, BlogBlog> implements IBlogBlogService {

    @Resource
    BlogBlogMapper blogBlogMapper;

    @Override
    public List<Map<String, Object>> getBlogById(Integer blog_id,Integer uId) {
        return blogBlogMapper.getBlogById(blog_id,uId);
    }

    @Override
    public int toDelBlogById(int blog_id) {
        return blogBlogMapper.toDelBlogById(blog_id);
    }

    @Override
    public int hiddenBlogById(int blog_id,int type) {
        return blogBlogMapper.hiddenBlogById(blog_id,type);
    }

    @Override
    public int noHiddenBlogById(int blog_id) {
        return blogBlogMapper.noHiddenBlogById(blog_id);
    }

    @Override
    public int toCreateBlog(BlogBlog blogBlog) {
        return blogBlogMapper.insert(blogBlog);
    }

    @Override
    public int toUpdateBlog(BlogBlog blogBlog) {
        UpdateWrapper<BlogBlog> wrapper = new UpdateWrapper<>();
        wrapper.eq("blog_id",blogBlog.getBlogId());
        return blogBlogMapper.update(blogBlog,wrapper);
    }


    @Override
    public PageInfo<Map<String, Object>> getSearchBlogJson(Integer index, Integer num, String searchText,Integer uId,Integer type,List<Integer> starIds) {
        PageHelper.startPage(index,num);
        return new PageInfo<>(blogBlogMapper.getSearchBlogJson(searchText,uId,type,starIds));
    }

    @Override
    public int addViewTime(int blogId) {
        return blogBlogMapper.addViewTime(blogId);
    }

    @Override
    public int toLikeOrUnlikeBlog(Integer blogId, Integer type, Integer uId) {
        return blogBlogMapper.toLikeOrUnlikeBlog(blogId,type,uId);
    }

    @Override
    public int editBlogNav(BlogBlog data) {
        data.setBlogNav(JSONArray.toJSONString(data.getBlogJsonNav()));
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return blogBlogMapper.toUpdate(data);
    }
}
